﻿using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace H2.NET
{
    class TaskAD
    {
        #region 属性 public

        public string SamName { get; set; }
        public string CN { get; set; }
        public string Name { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Initials { get; set; }
        public string Description { get; set; }
        public string StreetAddress { get; set; }
        public string PostOfficeBox { get; set; }
        public string Zip { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string CountryCode { get; set; }
        public string TelephoneNumber { get; set; }
        public string MobileNumber { get; set; }
        public string FaxNumber { get; set; }
        public string EMail { get; set; }
        public string WwwHomePage { get; set; }
        public string Title { get; set; }
        public string Department { get; set; }
        public string Company { get; set; }
        public string Manager { get; set; }
        protected internal string DN { get; set; }

        #endregion

        #region 构造 public
        public TaskAD(string domainDN, string samAccountName)
        {
            string basePath = string.Format(@"LDAP://{0}", domainDN);

            DirectoryEntry de = new DirectoryEntry(basePath);
            de.AuthenticationType = AuthenticationTypes.Secure;
            DirectorySearcher searcher = new DirectorySearcher(de);
            searcher.Filter = ("(&(objectclass=user)(objectcategory=person)(SAMAccountName=" + samAccountName + "))");
            searcher.SearchScope = SearchScope.Subtree;
			searcher.PropertiesToLoad.Add( @"userAccountControl" );
			searcher.PropertiesToLoad.Add( @"objectGUID" );
			searcher.PropertiesToLoad.Add( @"objectSid" );
			searcher.PropertiesToLoad.Add( @"sAMAccountName" );
			searcher.PropertiesToLoad.Add( @"cn" );
			searcher.PropertiesToLoad.Add( @"name" );
			searcher.PropertiesToLoad.Add( @"givenName" );
			searcher.PropertiesToLoad.Add( @"sn" );
			searcher.PropertiesToLoad.Add( @"initials" );
			searcher.PropertiesToLoad.Add( @"description" );
			searcher.PropertiesToLoad.Add( @"streetAddress" );
			searcher.PropertiesToLoad.Add( @"postOfficeBox" );
			searcher.PropertiesToLoad.Add( @"postalCode" );
			searcher.PropertiesToLoad.Add( @"c" );
			searcher.PropertiesToLoad.Add( @"st" );
			searcher.PropertiesToLoad.Add( @"l" );
			searcher.PropertiesToLoad.Add( @"telephoneNumber" );
			searcher.PropertiesToLoad.Add( @"mobile" );
			searcher.PropertiesToLoad.Add( @"facsimileTelephoneNumber" );
			searcher.PropertiesToLoad.Add( @"mail" );
			searcher.PropertiesToLoad.Add( @"wwwHomePage" );
			searcher.PropertiesToLoad.Add( @"title" );
			searcher.PropertiesToLoad.Add( @"department" );
			searcher.PropertiesToLoad.Add( @"company" );
			searcher.PropertiesToLoad.Add( @"manager" );

            SearchResult result = searcher.FindOne();

		    string dn = result.Path;            
		    if ( dn.StartsWith( basePath, StringComparison.InvariantCultureIgnoreCase ) )
		    {
			    dn = dn.Substring( basePath.Length ).Trim( '/' );
		    }

		    this.DN = dn;
            this.SamName = ReadFieldString(GetRP(result.Properties[@"sAMAccountName"]));
            this.CN = ReadFieldString(GetRP(result.Properties[@"cn"]));
            this.Name = ReadFieldString(GetRP(result.Properties[@"name"]));
            this.FirstName = ReadFieldString(GetRP(result.Properties[@"givenName"]));
            this.LastName = ReadFieldString(GetRP(result.Properties[@"sn"]));
            this.Initials = ReadFieldString(GetRP(result.Properties[@"initials"]));
            this.Description = ReadFieldString(GetRP(result.Properties[@"description"]));
            this.StreetAddress = ReadFieldString(GetRP(result.Properties[@"streetAddress"]));
            this.PostOfficeBox = ReadFieldString(GetRP(result.Properties[@"postOfficeBox"]));
            this.Zip = ReadFieldString(GetRP(result.Properties[@"postalCode"]));
            this.City = ReadFieldString(GetRP(result.Properties[@"l"]));
            this.State = ReadFieldString(GetRP(result.Properties[@"st"]));
            this.CountryCode = ReadFieldString(GetRP(result.Properties[@"c"]));
            this.TelephoneNumber = ReadFieldString(GetRP(result.Properties[@"telephoneNumber"]));
            this.MobileNumber = ReadFieldString(GetRP(result.Properties[@"mobile"]));
            this.FaxNumber = ReadFieldString(GetRP(result.Properties[@"facsimileTelephoneNumber"]));
            this.EMail = ReadFieldString(GetRP(result.Properties[@"mail"]));
            this.WwwHomePage = ReadFieldString(GetRP(result.Properties[@"wwwHomePage"]));
            this.Title = ReadFieldString(GetRP(result.Properties[@"title"]));
            this.Department = ReadFieldString(GetRP(result.Properties[@"department"]));
            this.Company = ReadFieldString(GetRP(result.Properties[@"company"]));
            this.Manager = ReadFieldString(GetRP(result.Properties[@"manager"]));
        }
        #endregion

        #region 方法 private
        private static object GetRP(ResultPropertyValueCollection rp)
        {
            if (rp == null)
            {
                return null;
            }
            else if (rp.Count <= 0)
            {
                return null;
            }
            {
                object o = rp[0];

                return o;
            }
        }

        private static string ReadFieldString(
            object fieldValue)
        {
            return
                fieldValue == null ||
                fieldValue == DBNull.Value ?
                string.Empty :
                Convert.ToString(fieldValue);
        }
        #endregion
    }
}
